package com.besmart.workflow.core.spike;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.besmart.workflow.core.exception.ConditionExtractException;
import com.besmart.workflow.core.exception.OperationNodeExtractException;
import com.besmart.workflow.core.exception.WorkflowNodeExtractException;
import com.besmart.workflow.core.model.ConditionNode;
import com.besmart.workflow.core.model.OperationNode;
import com.besmart.workflow.core.model.WorkflowNode;
import com.besmart.workflow.core.service.factory.WorkflowNodeFactory;

public class WorkFlowMapNodeFactoryUsageSpike {
	public static void main(String[] args) throws DocumentException, OperationNodeExtractException, ConditionExtractException, WorkflowNodeExtractException {
		SAXReader reader = new SAXReader();
		InputStream in = Runtime.getRuntime().getClass().getResourceAsStream("/workflow-config/workflowmap.xml");
		Document doc = reader.read(in);
		
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-context.xml");
		
		WorkflowNodeFactory factory = (WorkflowNodeFactory) context.getBean("workflowFactory");
		List<WorkflowNode> list = factory.createWorkflows(doc);
		for(WorkflowNode node : list)
		{
			List<OperationNode> ops = node.getOperations();
			System.out.println(node.getId());
			for(OperationNode op : ops)
			{
				List<ConditionNode> conditions = op.getConditions();
				System.out.println(" -"+op.getDescriptor().getId());
				for(ConditionNode condition : conditions)
				{
					System.out.println("    -"+condition.getTriggerSig());
				}
			}
		}
	}
}
